package com.ruoyi.datastatistics.Agreement;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.stereotype.Service;

@Service
public class HuiZhongV1 {
    public static void main(String[] args) {

    }

    /**
     * 解析meter数据
     * @param JSON
     * @return
     */
    public static JSONObject GetBufferPacketForMeter(JSONObject JSON)
    {

        JSONObject jo = new JSONObject();
        String packetType = JSON.getJSONObject("payload").getString("serviceId");
        JSONObject serviceData = JSON.getJSONObject("payload").getJSONObject("serviceData");
        if (serviceData != null)
        {
            try
            {
                jo = getDSDateinfoForMeter(serviceData);
                //jo = getDSDateinfoFromByte(bit);
            }
            catch (Exception e)
            {
                return null;
            }
            return jo;
        }
        else
        {
            return null;
        }

    }
    /**
     * 解析WaterMeter数据
     * @param JSON
     * @return
     */
    public static JSONObject GetBufferPacketForWaterMeter(JSONObject JSON)
    {

        JSONObject jo = new JSONObject();
        String packetType = JSON.getJSONObject("payload").getString("serviceId");
        JSONObject serviceData = JSON.getJSONObject("payload").getJSONObject("serviceData");
        if (serviceData != null)
        {
            try
            {
                jo = getDSDateinfoForWaterMeter(serviceData);
                //jo = getDSDateinfoFromByte(bit);
            }
            catch (Exception e)
            {
                return null;
            }
            return jo;
        }
        else
        {
            return null;
        }

    }


    /**
     * meter解析
     * @param serviceData
     * @return
     */
    public static JSONObject getDSDateinfoForMeter(JSONObject serviceData)
    {
        JSONObject jo = new JSONObject();
        jo.put("add", "");
        jo.put("type", 3);
        jo.put("type_msg", "汇中小表");//终端类型
        String voltage = Double.parseDouble(serviceData.getString("batteryVoltage"))/1000+"";
        jo.put("voltage", voltage);
        jo.put("signal1",  serviceData.getString("signalStrength"));
        String timeOfReading = serviceData.getString("timeOfReading");
        timeOfReading = timeOfReading.replaceAll("T","");
        timeOfReading = timeOfReading.replaceAll("z","");
        jo.put("date", timeOfReading.substring(2,8));//终端类型

        return jo;
    }

    /**
     * rWaterMeter解析
     * @param serviceData
     * @return
     */
    public static JSONObject getDSDateinfoForWaterMeter(JSONObject serviceData){
        JSONObject jo = new JSONObject();
        try{
            jo.put("add", "");
            jo.put("type", 3);
            jo.put("type_msg", "汇中小表");//终端类型
            String timeOfReading = serviceData.getString("timeOfReading");
            timeOfReading = timeOfReading.replaceAll("T","");
            timeOfReading = timeOfReading.replaceAll("Z","");

            String voltage = serviceData.containsKey("batteryVoltage")? Double.parseDouble(serviceData.getString("batteryVoltage"))/1000+"" :"";

            jo.put("voltage", voltage);
            jo.put("signal1",  serviceData.getString("signalStrength"));
            jo.put("date", timeOfReading.substring(2,8));//终端类型
            jo.put("TotalFlow", serviceData.getString("cumulativeFlow"));
            //正流量
            jo.put("ReadNumber", serviceData.getString("positiveCumulativeFlow"));
            //反流量
            jo.put("ReverseNumber", serviceData.getString("negativeCumulativeFlow"));
            JSONArray json_temp = new JSONArray();
            if (json_temp.size() == 0 )
            {
                JSONArray json_joc_2 = new JSONArray();
                JSONArray json_joc_3 = new JSONArray();
                JSONObject joc_2 = new JSONObject();
                try
                {

                    joc_2.put("TotalFlow",jo.getString("TotalFlow"));//累计向流量
                    joc_2.put("ReadNumber", jo.getString("ReadNumber"));//正向流量
                    joc_2.put("ReverseNumber", jo.getString("ReverseNumber"));//反向流量
                    joc_2.put("detailsTime", timeOfReading.substring(2,8));//反向流量
                    json_joc_2.add(joc_2);
                    JSONObject joc = new JSONObject();

                    joc.put("time", timeOfReading.substring(2,12));
                    joc.put("num", 1);
                    joc.put("dsinfo", json_joc_2);
                    //json_joc_3.Add(joc);
                    json_temp.add(joc);
                }
                catch (Exception e)
                {

                }
            }
            jo.put("info", json_temp);
        }catch (Exception e){

        }
        return jo;
    }



}
